<?php

namespace support;

use GuzzleHttp\Client;

class IDCard
{
    protected $appCode;

    public function __construct($appCode)
    {
        $this->appCode = $appCode;
    }

    public function auth($cardNo, $name)
    {
        $params = http_build_query([
            'cardNo' => $cardNo,
            'realName' => $name,
        ]);
        $client = new Client(['base_uri' => 'https://zidv2.market.alicloudapi.com']);
        $response = $client->request('GET', '/idcard/VerifyIdcardv2?' . $params, [
            'headers' => [
                'Authorization' => 'APPCODE ' . $this->appCode
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }

    public function ocrFront($base64Str)
    {
        $params = [
            'base64Str' => $base64Str,
        ];
        $client = new Client(['base_uri' => 'https://zidv2.market.alicloudapi.com']);
        $response = $client->request('POST', '/thirdnode/ImageAI/idcardfrontrecongnition', [
            'headers' => [
                'Authorization' => 'APPCODE ' . $this->appCode
            ],
            'form_params' => $params
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }

    public function ocrBack($base64Str)
    {
        $params = [
            'base64Str' => $base64Str,
        ];
        $client = new Client(['base_uri' => 'https://zidv2.market.alicloudapi.com']);
        $response = $client->request('POST', '/thirdnode/ImageAI/idcardbackrecongnition', [
            'headers' => [
                'Authorization' => 'APPCODE ' . $this->appCode
            ],
            'form_params' => $params
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }
}